<template>
    <el-card>
        <el-table border :data="skuArr" style="margin: 10px 0;">
            <el-table-column label="序号" width="80" align="center" type="index" />
            <el-table-column label="名称" prop="skuName" width="250px" />
            <el-table-column label="描述" prop="skuDesc" width="250px" />
            <el-table-column label="图片">
                <template #="{ row, $index }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="250px">

                <template #="{ row, $index }">
                    <div>
                        <el-button @click="updateSale(row)" :type="row.isSale == 1 ? 'danger' : 'primary'"
                            :icon="row.isSale == 1 ? Bottom : Top" size="small"></el-button>
                        <el-button :icon="Edit" size="small" type="primary"></el-button>
                        <el-button :icon="InfoFilled" size="small" type="info" @click="skuInfo(row)"></el-button>
                        <el-popconfirm :title="`你确定删除${row.skuName}?`" @confirm="deleteSku(row.id)">
                            <template #reference>
                                <el-button type="danger" :icon='Delete' size="small"></el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight" width="100px" />
            <el-table-column label="价格" prop="price" width="100px" />
        </el-table>
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]" background
            layout="prev, pager, next, jumper,->,sizes,total" :total="total" @size-change="sizeHandler"
            @current-change="getSkuList">
        </el-pagination>
        <el-drawer v-model="drawer" title="查看商品详情">
            <el-row>
                <el-col :span="6">名称</el-col>
                <el-col :span="18">{{ goods.skuName }}</el-col>
            </el-row><br>
            <el-row>
                <el-col :span="6">描述</el-col>
                <el-col :span="18">{{ goods.skuDesc }}</el-col>
            </el-row><br>
            <el-row>
                <el-col :span="6">价格</el-col>
                <el-col :span="18">{{ goods.price }}</el-col>
            </el-row><br>
            <el-row>
                <el-col :span="6">平台属性</el-col>
                <el-col :span="18">
                    <el-tag style="margin: 5px;" v-for="(item, index) in goods.skuAttrValueList" :key="index">{{
            item.attrName }}:{{ item.valueName }}</el-tag>
                </el-col>
            </el-row><br>
            <el-row>
                <el-col :span="6">销售属性</el-col>
                <el-col :span="18">
                    <el-tag style="margin: 5px;" v-for="(item, index) in goods.skuSaleAttrValueList" :key="index">
                        {{ item.saleAttrName }} : {{ item.saleAttrValueName }}
                    </el-tag>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="6">商品图片</el-col>
                <el-col :span="18">
                    <el-carousel :interval="4000" type="card" height="200px" indicator-position="outside">
                        <el-carousel-item v-for="(item, index) in goods.skuImageList" :key="index">
                            <img :src="item.imgUrl" style="width: 100%;height: 100%;">
                        </el-carousel-item>
                    </el-carousel>
                </el-col>
            </el-row>
        </el-drawer>
    </el-card>
</template>

<script setup lang="ts">
import { Delete, Bottom, Edit, InfoFilled, Top } from '@element-plus/icons-vue';
import { ref, onMounted } from 'vue';
import { reqRemoveSku, reqSkuList, reqSkuDetail, reqNotSale, reqSale } from '@/api/product/sku';
import { ElMessage } from 'element-plus';

const pageNo = ref<any>(1);
const pageSize = ref<any>(9);
//存储全部的SKU数组
const skuArr = ref<any>([]);
//存储全部SKU总数
const total = ref<any>(0);
// 右侧抽屉显隐
const drawer = ref<any>(false);
// SKU数据
const goods = ref<any>({})
onMounted(() => {
    getSkuList();
});
//获取SKU列表的方法
const getSkuList = async (val: number = 1) => {
    pageNo.value = val;
    //获取SKU列表的数据
    const result = await reqSkuList(pageNo.value, pageSize.value);
    skuArr.value = result.records;
    total.value = result.total;
}
//进行商品的上架与下架的操作
const updateSale = async (row: any) => {
    //当前商品正在售卖
    if (row.isSale == 1) {
        //下架
        await reqNotSale(row.id);
        //提示
        ElMessage({
            type: 'success',
            message: '下架成功'
        })
    } else {
        //当前商品下架了
        await reqSale(row.id);
        ElMessage({
            type: 'success',
            message: '上架成功'
        })
    }
    //获取更新后的数据
    getSkuList(pageNo.value);
}
// 分页器下拉菜单选项发生变化的时候会触发
const sizeHandler = (val: number) => {
    pageSize.value = val;
    getSkuList();
}
//点击查看商品详情按
const skuInfo = async (row: any) => {
    drawer.value = true
    const result = await reqSkuDetail(row.id);
    goods.value = result;
    console.log(result);
}
// 删除sku
const deleteSku = async (row: any) => {
    await reqRemoveSku(row);
    ElMessage({
        type: 'success',
        message: '删除成功'
    })
    getSkuList();
}
</script>

<script lang="ts">
name: 'Sku'
</script>

<style scoped>
:deep(4) {
    background-color: red;
    width: 5px;
    height: 5px;
    border-radius: 50%;
}
</style>